# Copyright 2014 The Native Client Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

include $(NACL_SDK_ROOT)/tools/common.mk

NACL_LDFLAGS += ${NACL_CLI_MAIN_LIB}
PNACL_LDFLAGS += ${NACL_CLI_MAIN_LIB}
TARGET = libgit2-demo

LIBS = git2 ssh2 ssl crypto z
ifeq ($(TOOLCHAIN),newlib)
LIBS += glibc-compat
endif
ifeq ($(TOOLCHAIN),pnacl)
LIBS += glibc-compat
endif
ifeq ($(TOOLCHAIN),clang-newlib)
LIBS += glibc-compat
endif
SOURCES = libgit2-demo.c operations.c
INSTALL_DIR = $(NACL_PACKAGES_PUBLISH)/$(TARGET)/$(TOOLCHAIN)
CFLAGS += -Wall -Werror

ifeq ($(TOOLCHAIN),pnacl)
EXEEXT=.pexe
else
EXEEXT=.nexe
endif

ifneq ($(TOOLCHAIN),pnacl)
# We want the nmf to contain all the .nexe we have previously
# built as well as the one currently being built, so we modify
# the list of executables that we pass to create_nmf.
EXECUTABLES += $(wildcard $(OUTDIR)/$(TARGET)_x86_64$(EXEEXT))
EXECUTABLES += $(wildcard $(OUTDIR)/$(TARGET)_x86_32$(EXEEXT))
EXECUTABLES += $(wildcard $(OUTDIR)/$(TARGET)_arm$(EXEEXT))
endif

# Build rules generated by macros from common.mk:

$(foreach src,$(SOURCES),$(eval $(call COMPILE_RULE,$(src),$(CFLAGS))))

ifeq ($(CONFIG),Release)
$(eval $(call LINK_RULE,$(TARGET)_unstripped,$(SOURCES),$(LIBS),$(DEPS)))
$(eval $(call STRIP_RULE,$(TARGET),$(TARGET)_unstripped))
else
$(eval $(call LINK_RULE,$(TARGET),$(SOURCES),$(LIBS),$(DEPS)))
endif

$(eval $(call NMF_RULE,$(TARGET),))

install:
	mkdir -p $(INSTALL_DIR)
	cp $(OUTDIR)/$(TARGET)*$(EXEEXT) $(INSTALL_DIR)
	rm -f $(INSTALL_DIR)/*_unstripped*$(EXEEXT)
	cp $(OUTDIR)/$(TARGET).nmf $(INSTALL_DIR)
ifeq ($(TOOLCHAIN),glibc)
	cp -r $(OUTDIR)/lib* $(INSTALL_DIR)
endif
	cp httpd.py $(INSTALL_DIR)
	cp index.html $(INSTALL_DIR)
	cp background.js $(INSTALL_DIR)
	cp libgit2.js $(INSTALL_DIR)
	cp manifest.json $(INSTALL_DIR)
ifeq ($(TOOLCHAIN),pnacl)
	sed -i.bak 's/x-nacl/x-pnacl/g' $(INSTALL_DIR)/index.html
endif
